package com.hs.jhzd.controller;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hs.jhzd.entity.PoliceIntoPrisonRecord;
import com.hs.jhzd.repository.PoliceIntoPrisonRecordRepository;
import com.hs.jhzd.utils.DateUtils;

@Controller
@RequestMapping("/pipr")
public class PoliceIntoPrisonRecordController {

  @Autowired
  private PoliceIntoPrisonRecordRepository repository;

  @Value("${jsbh}")
  private String jsbh;

  @RequestMapping("/search")
  @ResponseBody
  public List<PoliceIntoPrisonRecord> search(String policeName,
      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {

    if (startTime == null) {
      startTime = DateUtils.getStartOfDay(new Date());
    }

    if (endTime == null) {
      endTime = DateUtils.getEndOfDay(new Date());
    }
    if (StringUtils.isBlank(policeName)) {
      return repository.findByTime(startTime, endTime);
    }
    return repository.findByPoliceNameAndTime(policeName, startTime, endTime);
  }

  @RequestMapping("/admin/main")
  public String rollcallMainPage(Model model, String[] jshs, String policeName,
      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, Boolean swipe) {
    List<PoliceIntoPrisonRecord> policeIntoPrisonRecords = search(policeName, startTime, endTime);
    model.addAttribute("jsbh", jsbh);
    model.addAttribute("policeName", policeName);
    model.addAttribute("results", policeIntoPrisonRecords);
    return "police-into-prison-record-list";
  }
}
